package cn.k7g.alloytest.controller;


import cn.k7g.alloy.annotation.AlloyContent;
import cn.k7g.alloy.annotation.Label;
import cn.k7g.alloy.annotation.TimeExtra;
import cn.k7g.alloy.expose.AlloyContentHandler;
import cn.k7g.alloytest.UserVo;
import cn.k7g.alloytest.alloycontenthandler.HelloAlloyContentHandler;
import cn.k7g.alloytest.alloycontenthandler.WorldAlloyContentHandler;
import cn.k7g.alloytest.exception.LoginFreezeException;
import cn.k7g.alloytest.exception.LoginUserNotExistsException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;

@Slf4j
@RestController
@Validated
public class IndexController {

    @Autowired
    private ObjectMapper objectMapper;


    @GetMapping("get")
    public Object get(
            @AlloyContent(HelloAlloyContentHandler.BEAN_NAME) @RequestParam Integer id1,
            @AlloyContent(WorldAlloyContentHandler.BEAN_NAME) @RequestParam Integer id2
    ) {
        return id1 + " -- "+ id2;
    }

    @GetMapping("get2")
    public Object get2(
            @AlloyContent(HelloAlloyContentHandler.BEAN_NAME) @RequestParam Integer id1,
            @AlloyContent(WorldAlloyContentHandler.BEAN_NAME) @RequestParam Integer id2
    ) {
        return id1 + " -- "+ id2;
    }


    @GetMapping("set")
    public Object set() throws Exception {
//        return new User();
        return objectMapper.writeValueAsString(new User());
    }
    @GetMapping("set2")
    public Object set2() throws Exception {
//        return new User();
        return objectMapper.writeValueAsString(new User2());
    }


    @GetMapping("/getData")
    public String getData(@RequestParam("id") @Label("年龄") @Min(value = 1) int age, @Label("姓名") @NotEmpty String name) {
        // 在此处处理业务逻辑
        return "Received ID: " + age;
    }





    @GetMapping("login")
    public Object login(String username) throws Exception {
        if ("admin".equals(username)) {
            throw new LoginFreezeException(username, 7);
        } else {
            throw new LoginUserNotExistsException(username);
        }
//        throw new LoginException();
    }



    @PostMapping("post")
    public Object post(@RequestBody UserVo vo) throws Exception {
//        return new User();
        return objectMapper.writeValueAsString(new User());
    }

    @RequestMapping("hello1/{id}")
    public String hello1(@PathVariable @Size(max = 2) @Label("编号") String id) {
        return id;
    }

//    @RequestMapping("hello2")
//    public String hello2(@AlloyContent String id) {
//        return id;
//    }

    @Autowired
    AlloyContentHandler handler;

    @RequestMapping("hello3/{id}")
    public String hello3(@AlloyContent @PathVariable Integer id, @AlloyContent @RequestParam("aa") Integer aa) throws JsonProcessingException {
        return id.toString();
    }

    @PostMapping("form/post")
    public Object post(User vo) {
        return vo.id1 + " -- " + vo.id2;
    }

    @Getter
    @Setter
    public static class User {

        public User () {
            System.out.println("111");
        }

        @AlloyContent(HelloAlloyContentHandler.BEAN_NAME)
        @Label("高级ID1")
        private Long id1 = 1L;
        @AlloyContent(WorldAlloyContentHandler.BEAN_NAME)
        private Long id2 = 1L;

        @Label("高级User2")
        private User2 user2;

//        @JsonFormat(pattern = "uuuu==MM==dd HH:mm:ss")
        @TimeExtra(ago = true)
        private LocalDateTime birder = LocalDateTime.now().withYear(2023);
    }


    @Getter
    @Setter
    public static class User2 {
        @AlloyContent(WorldAlloyContentHandler.BEAN_NAME)
        @Label("属性ID1")
        private Long id1 = 1L;
        @AlloyContent
        private Long id2 = 1L;
    }

}
